package com.wcity.manage.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * HttpResponse工具
 *
 * @author dy
 */
public class RespUtil {


    /**
     * 发送JSON内容(utf-8)
     *
     * @param response
     * @param text
     */
    public static void sendJson( HttpServletResponse response, String text ) {
        send( response, "application/json;charset=UTF-8", text );
    }

    /**
     * 发送内容
     *
     * @param response
     * @param contextType
     * @param text
     */
    public static void send( HttpServletResponse response, String contextType, String text ) {
        response.setContentType( contextType );
        response.setHeader( "Pragma", "No-cache" );
        response.setHeader( "Cache-Control", "no-cache" );
        response.setDateHeader( "Expires", 0 );
        try {
            response.getWriter().print( text );
        } catch ( IOException e ) {
            log.error( "send error... {}", e.getMessage(), e );
        }
    }

    /**
     * 重定向
     *
     * @param response
     * @param url
     * @return
     */
    public static String redirect( HttpServletResponse response, String url ) {
        return "redirect:" + url;
    }

    /**
     * 转发
     *
     * @param response
     * @param url
     * @return
     */
    public static String forward( HttpServletResponse response, String url ) {
        return "forward:" + url;
    }

    private final static Logger log = LoggerFactory.getLogger( RespUtil.class );

}
